#pragma once

#include "event_loop.h"
#include "event_loop_thread.h"
#include "noncopyable.h"

#include <functional>
#include <memory>
#include <string>
#include <vector>

class EventLoop;
class EventLoopThread;

class EventLoopThreadPool : noncopyable {
  public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;

    EventLoopThreadPool(EventLoop *base_loop, const std::string &name_arg);
    ~EventLoopThreadPool();

    void set_thread_num(int num_threads) { num_threads_ = num_threads; }

    void start(const ThreadInitCallback &cb = ThreadInitCallback());

    // 如果工作在多线程，base_loop_默认以轮询的防止分配channel给subloop
    EventLoop *get_next_loop();

    std::vector<EventLoop *> get_all_loops();

    bool started() const { return started_; }
    const std::string name() const { return name_; }

  private:
    EventLoop *base_loop_; // EventLoop loop
    std::string name_;
    bool started_;
    int num_threads_;
    int next_;
    std::vector<std::unique_ptr<EventLoopThread>> threads_;
    std::vector<EventLoop *> loops_;
};